package com.hlingsoft.cms.action;

import com.hlingsoft.cms.dao.*;
import com.hlingsoft.cms.entity.AppUser;
import com.hlingsoft.cms.entity.Comment;
import com.hlingsoft.cms.entity.CommentMessage;
import com.hlingsoft.cms.entity.MessageStatus;
import com.hlingsoft.cms.util.Common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by selbylei on 2015/5/6.
 */
@Controller
@RequestMapping("/comment")
public class CommentAction extends BaseAction  {

    @Autowired
    private CommentDao commentDao;

    @Autowired
    private NewsDao newsDao;

    @Autowired
    private CommentMessageDao commentMessageDao;

    @Autowired
    private MessageStatusDao messageStatusDao;


    /**
     * 添加评论
     * @param myId
     * @param newsId
     * @param content
     * @param request
     * @return
     */
    @RequestMapping(value = "/add.json")
    @ResponseBody
    public Map<String, Object> add(
            @RequestParam(value = "myId") long myId,
            @RequestParam(value = "newsId") long newsId,
            @RequestParam(value = "content") String content,
            @RequestParam(value = "toId",required = false) Integer toId,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            Date now = new Date();

            Comment comment = new Comment();
            comment.setMyId(myId);
            if (toId!=null){
                comment.setToId(toId.longValue());
                comment.setType(1);
            }else {
                comment.setToId(newsDao.getNewsById(newsId).getAppUserId());
                comment.setType(0);
            }
            comment.setContent(Common.encodeStr(content));
            comment.setNewsId(newsId);
            comment.setCreateTime(now);
            comment.setUpdateTime(now);
            commentDao.addComment(comment);

            CommentMessage commentMessage =new CommentMessage();
            commentMessage.setMyId(myId);
            commentMessage.setToId(comment.getToId());
            commentMessage.setContent(comment.getContent());
            commentMessage.setNewsId(newsId);
            commentMessage.setCreateTime(now);
            commentMessage.setUpdateTime(now);
            commentMessageDao.addCommentMessage(commentMessage);


            MessageStatus messageStatus = new MessageStatus();
            messageStatus.setAppUserId(comment.getToId());
            messageStatus.setMessageId(commentMessage.getId());
            messageStatus.setMessageType(1);
            messageStatusDao.addMessageStatus(messageStatus);


            resultMap.put("comment",comment);
            resultMap.put("result",true);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }




}
